package com.qfedu.ssm.controllers;

import com.google.gson.Gson;
import com.qfedu.ssm.handler.ZanHandler;
import com.qfedu.ssm.pojos.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/zan")
public class ZanController {
    @Autowired
    private ZanHandler zanHandler;

    @RequestMapping("/add.do")
    @ResponseBody
    public String add(HttpServletRequest request,String itemId,String type) {
        User user = (User) request.getSession().getAttribute("user");
        int userId = user.getId();
        try {
            zanHandler.addZan(itemId, userId,type);
        } catch (Exception e) {
            e.printStackTrace();
        }
        long count = zanHandler.countZan(itemId,type);
        return new Gson().toJson(count);
    }

}
